package com.fozu.office.sale.web;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

/*
 *销售计划 
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fozu.common.config.Constants;
import com.fozu.common.pojo.ResultVO;
import com.fozu.common.util.IdGen;
import com.fozu.office.sale.entity.ChanceVO;
import com.fozu.office.sale.entity.PlanVO;
import com.fozu.office.sale.service.ChanceServiceImpl;
import com.fozu.office.system.entity.UserVO;
/*
 * 机会管理
 */
@Controller
@RequestMapping(value="chance")
public class ChanceController {
	@Autowired
	private ChanceServiceImpl chanceService;
	
	//跳转到机会列表
	@RequestMapping(value="chanceList.shtml")
	public String toPlanList(){
		return "sale/chance/chanceList";
	}
	//跳转到修改页面
	@RequestMapping(value="chanceUpdate.shtml")
	public String toUpdateChane(){
		return "sale/chance/chanceUpdate";
	}
	//跳转到添加页面
	@RequestMapping(value="chanceAdd.shtml")
	public String toAddPlan(){
		return "sale/chance/chanceAdd";
	}
	//跳转到查看页面
	@RequestMapping(value="chanceQuery.shtml")
	public String toAuditPlan(){
		return "sale/chance/chanceQuery";
	}
	//跳转到分配页面
	@RequestMapping(value="distribution.shtml")
	public String toDistribution(){
		return "sale/chance/distribution";
	}
	//机会列表
	@PostMapping(value="getChanceList")
	@ResponseBody
	public ResultVO getChanceList(@RequestParam(value="pageNo") Long pageNo,
								  @RequestParam(value="pageSize") Long pageSize,
								  @RequestParam(value="createBy", required=false ) String createBy){
		try{
			ChanceVO chance= new ChanceVO();
			chance.setCreateBy(createBy);
			IPage <ChanceVO> ipage = chanceService.listChance(new Page<ChanceVO>(pageNo,pageSize), chance);
			return new ResultVO(Constants.status_SUCCESS,"获取成功",ipage);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"获取失败",null);	
		}
	}
	//通过Id拿到对象
	@PostMapping(value="getChanceById")
	@ResponseBody
	public ResultVO getChanceById(@RequestParam(value="chanceId") String chanceId){
		try{
			ChanceVO chance = new ChanceVO();
			chance.setId(chanceId);
			ChanceVO ch = chanceService.getChanceById(chance);
			return new ResultVO(ch);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"获取失败",null);
		}
	}
	//修改
	@PostMapping(value="updateChance")
	@ResponseBody
	public ResultVO updateChance(HttpServletRequest request,
							   @RequestParam(value="id",required=true) String id,
							   @RequestParam(value="chanceName",required=true) String chanceName,
							   @RequestParam(value="type",required=true) Integer type,
							   @RequestParam(value="content",required=true) String content,
							   @RequestParam(value="remarks",required=false) String remarks){
		try{
			ChanceVO chance= new ChanceVO(id,chanceName,type,content,remarks);
			UserVO loginUser = (UserVO) request.getSession().getAttribute("loginUser");
			chance.setUpdateDate(new Date());
			chance.setUpdateBy(loginUser.getId());
			chanceService.updateChance(chance);
			return new ResultVO(Constants.status_SUCCESS,"修改成功",null);
		}catch (Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"修改失败",null);
		}
	}
	//添加
	@PostMapping(value="doAddChance")
	@ResponseBody
	public ResultVO doAddChance(HttpServletRequest request,
							   @RequestParam(value="chanceName",required=true) String chanceName,
							   @RequestParam(value="type",required=true) Integer type,
							   @RequestParam(value="content",required=true) String content,
							   @RequestParam(value="remarks",required=false) String remarks){
		try{
			String id= IdGen.getId();
			UserVO loginUser = (UserVO) request.getSession().getAttribute("loginUser");
			String createBy = loginUser.getId();
			ChanceVO chance= new ChanceVO(id,chanceName,type,content,remarks);
			chance.setCreateBy(createBy);
			chance.setCreateDate(new Date());
			chance.setUpdateDate(new Date());
			chance.setUpdateBy(createBy);
			chanceService.addChance(chance);
			return new ResultVO (Constants.status_SUCCESS,"添加成功",null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"添加失败",null);
		}
	}
	//分配
	@PostMapping(value="dis")
	@ResponseBody
	public ResultVO dis(@RequestParam(value="distribution",required=true)String distribution,
						@RequestParam(value="id",required=true)String id){
		try{
			ChanceVO chance = new ChanceVO();
			chance.setId(id);
			chance.setDistribution(distribution);
			chanceService.distribut(chance);
			return new ResultVO(Constants.status_SUCCESS,"分配成功",null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"分配失败",null);
		}
	}
}
